<template>
	<view>

		<u-cell-group v-for="(val, key,index) in urls" :key="index" :title="'附件：'+key" :border="true">
			<u-album :urls="val" :multipleSize="70"></u-album>



		</u-cell-group>
		<u-cell-group :border="true" title="附件:">

			<u-cell v-for="(file,index) in files" :key="index" :title="file.Name"
				:label="file.MyType+','+file.CreateTime" isLink @click="download(file)"></u-cell>

		</u-cell-group>

	</view>
</template>

<script>
	import {
		getfilebyoutid
	} from '@/common/api.js'
	import {
		baseUrl
	} from '@/common/config.js'
	export default {
		props: {
			outid: {}
		},
		data() {
			return {
				urls: [],
				files: [],

				mydata: {
					Name: '',
					Dept: {
						Name: ''
					}
				}
			}
		},
		watch: {

			outid: {

				handler(newv, oldv) {
					this.getdate()

				}

			}

		},
		onLoad(option) {
			// this.buildid = option.buildid
			// if (option.buildid)

		},
		mounted() {
			this.getdate()
		},

		methods: {
			showmap() {
				uni.navigateTo({
					url: 'map?buildid=' + this.buildid
				})
			},
			getFileExtension(filename) {
				return filename.split(".").pop().toLowerCase()
			},

			getdate() {

				// this.mydata={Name:'22',Addr:'',ProjInfo:{Name:''},BuildManager:""}
				getfilebyoutid(this.outid).then(res => {
					// this.mydata={Name:'33',Addr:'',ProjInfo:{Name:''},BuildManager:""}
					// console.log(this.data)
					var iurls = {}
					var ifiles = []
					res.forEach(e => {
						var ext = this.getFileExtension(e.Name)
						if (ext == "jpg" || ext == "png" || ext == "bmp" || ext == "gif") {
							if (!iurls[e.MyType]) {
								iurls[e.MyType] = []
							}
							iurls[e.MyType].push(`${baseUrl}/SysFile/download?id=${e.Id}`)
							// iurls.push(`${baseUrl}/SysFile/download?id=${e.Id}`)
						} else
							ifiles.push(e)
					})

					this.urls = iurls
					this.files = ifiles
					// this.mydata = res.data[0];
					// console.log(this.data)
				})
			},
			download(file) {
				var url = `${baseUrl}/SysFile/download?id=${file.Id}`
				uni.downloadFile({
					url: url, //下载地址接口返回
					success: (data) => {
						if (data.statusCode === 200) {
							//文件保存到本地
							uni.saveFile({
								tempFilePath: data.tempFilePath, //临时路径
								success: function(res) {
									uni.showToast({
										icon: 'none',
										mask: true,
										title: '文件已保存：' + res.savedFilePath, //保存路径
										duration: 3000,
									});
									setTimeout(() => {
										//打开文档查看
										uni.openDocument({
											filePath: res.savedFilePath,
											success: function(res) {
												// console.log('打开文档成功');
											}
										});
									}, 3000)
								}
							});
						}
					},
					fail: (err) => {
						console.log(err);
						uni.showToast({
							icon: 'none',
							mask: true,
							title: '失败请重新下载',
						});
					},
				});
			},
		}
	}
</script>

<style>

</style>